/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.jobScheduler;

import java.util.List;

import org.nudt.jCloud.infrastructure.Slot;
import org.nudt.jCloud.infrastructure.SlotType;
import org.nudt.jCloud.simulation.Request;
import org.nudt.jCloud.simulation.RequestData;
import org.nudt.jCloud.simulation.SchedBatchSet;
import org.nudt.jCloud.simulation.VirtSlotAlloc;
import org.nudt.jCloud.workload.Task;

public class SJFSGangScheduler extends JobScheduler{	

	public SJFSGangScheduler(String name) {
		super(name);
	}

	@Override
	public void assignTaskR(RequestData requestData, SchedBatchSet schedBatchSet) {
		requestData.sortBySizeInc();
		List<Request> requestList = requestData.getRequestList();
		for(int i=0; i<requestList.size(); ++i){
			Request request = requestList.get(i);
			SlotType slotType = request.getJob().getSlotType();
			int unAssignedTaskNum = request.getUnAssignedTasks().size();
			for(int j=0; j<unAssignedTaskNum; ++j){
				Task task = request.getUnAssignedTasks().get(0);
				Slot slot = getFrwk().getOneFreeSlot(slotType);
				if(slot != null){
					assignTasktoSlot(request, task, slot, schedBatchSet);
				}else{
					break;
				}
			}
			if(request.getUnAssignedTasks().size()>0){
				cancelRequestR(request,schedBatchSet);
			}
		}
		
	}

	@Override
	public void assignTaskS(RequestData requestData, VirtSlotAlloc virtSlotAlloc) {
		
	}
			
}
